package com.qdxwx.manage.controller.website;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.website.WebsiteNavigateService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.website.WebsiteNavigate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 导航Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/navigate")
public class WebsiteNavigateController extends BaseController {
    private final String prefix = "navigate";

    @Autowired
    private WebsiteNavigateService websiteNavigateService;

    /**
     * 导航列表
     */
    @GetMapping("/list.html")
    public String navigate(HttpServletRequest request, ModelMap mmap) {
        String modeType = TKString.trim(request.getParameter("modeType"));
        if (!modeType.equals("tab") && !modeType.equals("friend") && !modeType.equals("footer")) {
            modeType = "";
        }
        mmap.put("modeType", modeType);
        return "navigate/navigate-list";
    }

    /**
     * 友情链接列表
     */
    @GetMapping("/friend/list.html")
    public String Friend(ModelMap mmap) {
        mmap.put("modeType", "friend");
        return "navigate/navigate-list";
    }

    /**
     * 查询导航列表
     */
    @Permissions("navigate:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        String type = request.getParameter("modeType");
        WebsiteNavigate websiteNavigate = new WebsiteNavigate();
        if (!TKString.isEmpty(type)) {
            websiteNavigate.setType(type);
        }
        List<WebsiteNavigate> list = websiteNavigateService.getList(websiteNavigate, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(websiteNavigateService.count(websiteNavigate));
        return rspData;
    }


    /**
     * 新增导航
     */
    @GetMapping("/add.html")
    public String add() {
        return "navigate/navigate-add";
    }

    /**
     * 新增保存导航
     */
    @Permissions("navigate:list")
    @Log(title = "添加导航", businessType = BusinessType.INSERT)
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request) {
        String type = request.getParameter("type");
        if (TKString.isEmpty(type)) return error("请选择导航类型");
        String name = request.getParameter("name");
        if (TKString.isEmpty(name)) return error("请输入导航名称");
        String url = request.getParameter("url");
        if (TKString.isEmpty(url)) return error("请输入跳转链接");
        WebsiteNavigate websiteNavigate = new WebsiteNavigate();
        websiteNavigate.setType(type);
        websiteNavigate.setName(name);
        websiteNavigate.setUrl(url);
        websiteNavigate.setNewPage(TKConvert.toInteger(request.getParameter("newPage")));
        websiteNavigate.setSort(TKConvert.toInteger(request.getParameter("sort")));
        websiteNavigate.setImage(request.getParameter("image"));
        websiteNavigate.setStatus(0);
        return toAjax(websiteNavigateService.insert(websiteNavigate));
    }

    /**
     * 修改导航
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error404;
        WebsiteNavigate websiteNavigate = websiteNavigateService.get(id);
        if (websiteNavigate == null) return error404;
        mmap.put("websiteNavigate", websiteNavigate);
        return "navigate/navigate-edit";
    }

    /**
     * 修改保存导航
     */
    @Permissions("navigate:list")
    @Log(title = "编辑导航", businessType = BusinessType.UPDATE)
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error("缺少id参数");
        WebsiteNavigate websiteNavigate = websiteNavigateService.get(id);
        if (websiteNavigate == null) return error("找不到指定数据");
        String type = request.getParameter("type");
        if (TKString.isEmpty(type)) return error("请选择导航类型");
        String name = request.getParameter("name");
        if (TKString.isEmpty(name)) return error("请输入导航名称");
        String url = request.getParameter("url");
        if (TKString.isEmpty(url)) return error("请输入跳转链接");
        websiteNavigate.setType(type);
        websiteNavigate.setName(name);
        websiteNavigate.setUrl(url);
        websiteNavigate.setNewPage(TKConvert.toInteger(request.getParameter("newPage")));
        websiteNavigate.setSort(TKConvert.toInteger(request.getParameter("sort")));
        websiteNavigate.setImage(request.getParameter("image"));
        return toAjax(websiteNavigateService.update(websiteNavigate));
    }

    /**
     * 导航禁用与启用
     */
    @PostMapping("/status.json")
    @Permissions("navigate:list")
    @Log(title = "导航状态", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult status(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error("缺少id参数");
        WebsiteNavigate websiteNavigate = websiteNavigateService.get(id);
        if (websiteNavigate == null) return error("找不到指定数据");
        websiteNavigate.setStatus(TKConvert.toInteger(request.getParameter("status")));
        return toAjax(websiteNavigateService.updateStatus(websiteNavigate));
    }

    /**
     * 删除导航
     */
    @Permissions("navigate:list")
    @Log(title = "删除导航", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if (id < 1) return error("参数异常");
        return toAjax(websiteNavigateService.delete(id));
    }
}
